//
//  DDCZEmoticon.swift
//  XLWB
//
//  Created by MacBook on 16/7/10.
//  Copyright © 2016年 MacBook. All rights reserved.
//

import UIKit
import YYModel
class DDEmoticon: NSObject {
    // 表情类型
    var type = false

    // 表情字符串
    var chs: String?
    // 表情图片名称
    var png: String?
    // emoji 的十六进制编码
    var code: String?
    // 表情模型所在的目录
    var directory: String?
    // `图片`表情对应的图像
    var image: UIImage? {
        
        // 判断表情类型
        if type {
            return nil
        }
        
        guard let directory = directory,
            png = png,
            path = Bundle.main.pathForResource("HMEmoticon.bundle", ofType: nil),
            bundle = Bundle(path: path)
            else {
                return nil
        }
        return UIImage(named: "\(directory)/\(png)", in: bundle, compatibleWith: nil)
    }
    // 将当前的图像转换生成图片的属性文本
    func imageText(font: UIFont) -> AttributedString {
        
        // 1. 判断图像是否存在
        guard let image = image else {
            return AttributedString(string: "")
        }
        
        // 2. 创建文本附件
        let attachment = NSTextAttachment()
        
        attachment.image = image
        let height = font.lineHeight
        attachment.bounds = CGRect(x: 0, y: -4, width: height, height: height)
        
        // 3. 返回图片属性文本
        return AttributedString(attachment: attachment)
    }
    
    override var description: String {
        return yy_modelDescription()
    }


}
